<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :close-on-click-modal="false" width="850px" append-to-body>
    <div class="form-box">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="100px" class="demo-ruleForm">
        <!-- 类型  编号 手机号码 保管人员 备注 -->
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option v-for="(dict, i) in type" :key="i" :label="dict.name" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="快捷语" prop="content">
          <el-input type="textarea" maxlength="500" rows="5" show-word-limit v-model="form.content" placeholder="请输入快捷语"
            style="width: 600px;" />
        </el-form-item>
        <!-- <el-form-item label="手机号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="保管人员" prop="custodian">
          <el-input v-model="form.custodian" placeholder="请输入保管人员" />
        </el-form-item> -->
        <!-- <span v-if="billTypeConfig&&billTypeConfig.length">
          <span v-for="custom in billTypeConfig" :key="custom.id">
            <el-form-item :label="custom.name" :prop="custom.key" v-if="custom.isShow">
              <el-input v-model="form[custom.key]" :placeholder="`请填写${custom.name}`" />
            </el-form-item>
          </span>
        </span> -->
        <!-- <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请填写备注"
              :rows="4"
              style="width:600px"
            />
          </el-form-item>
        </el-row> -->

        <div class="center">
          <el-button @click="close()">取消</el-button>
          <el-button type="primary" @click="submitForm('form')" v-loading="loading">提交</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
import quickwordApi from '../../../api/quickWords';
export default {
  name: "addFina",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value && value.length != 11) {
        callback(new Error('请输入11位手机号'));
      } else {
        callback();
      }
    };
    return {
      pageType: 'merchant',
      dialogVisible: false,
      loading: false,
      title: "新增快捷语",
      isEdit: false,
      /**销售客服 saleCustomer

通用设置 commonCustomer

供货客服 supplyCustomer

普通用户 ordinaryUser

包赔商 underwriter */
      type: [
        {
          name: "供货客服",
          value: 'supplyCustomer'
        }

      ],

      // 表单参数
      form: {
        type: 'supplyCustomer',
        content: ""
      },
      rules: {
        type: [
          { required: true, message: "请选择类型", trigger: "change" }
        ],
        content: [
          { required: true, message: "请输入快捷语", trigger: "change" }
        ]
      }
    };
  },
  methods: {
    // 打开弹窗
    open(flag, data, pageType) {
      this.pageType = pageType
      if (pageType == 'merchant') {
        this.type = [
          {
            name: "供货客服",
            value: 'supplyCustomer'
          }
        ]
        this.form.type = "supplyCustomer"
      } else if (pageType == 'baopei') {
        this.type = [
          {
            name: "包赔商",
            value: 'underwriter'
          }
        ]
        this.form.type = "underwriter"
      } else {
        this.type = [
          {
            name: "通用设置",
            value: 'commonCustomer'
          },
          {
            name: "销售客服",
            value: 'saleCustomer'
          },
          {
            name: "供货客服",
            value: 'supplyCustomer'
          },
          {
            name: "普通用户",
            value: 'ordinaryUser'
          },
          {
            name: "包赔商",
            value: 'underwriter'
          }
        ]
        this.form.type = ''
      }
      if (flag == "edit") {
        this.title = "修改快捷语";
        this.isEdit = true;
        this.handleUpdate(JSON.parse(JSON.stringify(data)));
      } else {
        this.isEdit = false;
      }
      this.dialogVisible = true;
    },

    // 重置
    reset() {
      this.$refs.form.resetFields();
    },

    /** 查询详情进行回显 */
    handleUpdate(row) {
      this.form = row
    },
    submitForm(formName) {
      this.$refs["" + formName].validate(valid => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            quickwordApi.editWord(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              this.$parent.getList();
            });

          } else {
            // 新增
            console.log(this.form);
            quickwordApi.addWord(this.form).then(res => {
              this.$message.success(res.msg);
              this.reset()
              this.dialogVisible = false;
              if (this.$parent.getList) {
                this.$parent.getList();
              } else {
                this.$emit('add')
              }

            });
          }
        } else {
          this.$alert("请填写完整！");
        }
      });
    },

    close() {
      this.reset()
      this.dialogVisible = false;
    }
  }
};
</script>
<style scoped lang="css">
.center {
  text-align: center;
}

.form-box {
  /* max-height: calc(100vh - 20vh - 100px);
  overflow-y: auto;
  overflow-x: hidden; */
  padding-top: 20px;
}

.form-box>>>.el-input {
  width: 250px;
}

.form-box>>>img {
  width: 100%;
}

.form-box>>>.el-upload-dragger {
  width: 148px;
  height: 148px;
}



.form-box>>>.el-form-item__content {
  max-width: 655px;
}
</style>
